package com.toKnow.controller;

import com.toKnow.model.dto.AuthorInfoDTO;
import com.toKnow.model.dto.UserInfoDTO;
import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.Source;
import com.toKnow.model.vo.*;
import com.toKnow.model.vo.transfer.UserInfoVOTransfer;
import com.toKnow.service.ArticleService;
import com.toKnow.service.ServiceException;
import com.toKnow.service.SourceService;
import com.toKnow.service.UserService;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/author")
public class AuthorController {
    @Autowired
    ArticleService articleService;
    @Autowired
    SourceService sourceService;
    @Autowired
    UserService userService;
    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }



    /**
     * 作者信息，返回除了userpwd、activateCode以外的所有信息
     * /user/info/show
     * GET{user_id:Integer}{commonResult}
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public BasicVo showContentById(HttpSession session, Integer author_id){
        if(author_id == null) {
            return new BasicVo(false, "没有携带作者id", null);
        }

        Integer uid = getUid(session);
        BasicVo cm = new BasicVo();
        cm.setSuccess(false); //默认为false
        AuthorInfoDTO authorInfoDTO = userService.getAuthorInfo(uid, author_id);
        AuthorInfoVo vo = new AuthorInfoVo(authorInfoDTO);
        cm.setSuccess(true);
        cm.setMessage("");
        cm.setObject(vo);
        return cm;
    }

    @RequestMapping(value = "/article",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getCollection(HttpSession session,RestrictionVo restrictionVo){
        try {
            List<Article> articles = articleService.searchArticle(getUid(session),restrictionVo);
            List<ArticleAbstractVo> ans = new ArrayList<>();
            for (Article article : articles) {
                ans.add(new ArticleAbstractVo(article));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }

    @RequestMapping(value = "source",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getWorkSource(HttpSession session, RestrictionVo restrictionVo){
        try {
            Integer uid = getUid(session);
            List<Source> sources = sourceService.searchSource(uid,restrictionVo);
            List<SourceAbstractVo> ans = new ArrayList<>();
            for (Source source : sources) {
                ans.add(new SourceAbstractVo(source));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }

    // 关注
    // transactional right
    @RequestMapping(value = "follow", method = RequestMethod.POST)
    @ResponseBody
    public BasicVo follow(HttpSession session, Integer author_id) {
        BasicVo cm = new BasicVo();
        Integer uid = getUid(session);
        Boolean flag = userService.checkFollow(uid, author_id);
        if(flag) {
            Boolean res = userService.unfollow(uid, author_id);
            if(res) {
                cm.setSuccess(true);
                cm.setMessage("取消关注成功");
            } else {
                cm.setSuccess(false);
                cm.setMessage("取消关注失败");
            }
        } else {
            Boolean res = userService.follow(uid, author_id);
            if(res) {
                cm.setSuccess(true);
                cm.setMessage("关注成功");
            } else {
                cm.setSuccess(false);
                cm.setMessage("关注失败");
            }
        }
        return cm;
    }
}
